$(function () {
  var $p = $('#p');
  var $texts = $("#texts");
  var $password = $("#password");
  var $copy_password = $("#copy_password");
  $texts.blur(function () {
    var textss = $texts.val();
    var regular = /^[a-zA-Z0-9_-]{4,16}$/;
    if (textss == ""){
      $p.text("用户名不能为空").css("color","red").css("display","block");
      $(this).focus();
      return false;
    }else if (regular.test(textss) == false){
      $p.text("你输入的用户名不正确，请字母开头，4到16位").css("color","red").css("display","block");
      $(this).focus();
      return false;
    }else {
      $p.text('输入正确').css("display","block").css("color","green");
    }
  })

  $("#password").blur(function () {
    var password = $password.val();
    var mima = /^[a-z0-9_-]{6,18}$/;
    if (password == ""){
      $p.text("密码不能为空").css("color","red").css("display","block");
      $(this).focus();
      return false;
    }else if (mima.test(password) == false){
      $p.text("你输入的密码不正确，请字母开头，4到16位").css("color","red").css("display","block");
      $(this).focus();
      return false;
    }else {
      $p.text('输入正确').css("display","block").css("color","green");
    }
  })

  $("#copy_password").blur(function () {
    var password = $password.val();
    var copy_password = $copy_password.val();
    if (password!=copy_password){
      $p.text("你输入的重复密码不正确,请重新输入").css("color","red").css("display","block");
      $(this).focus();
      return false;
    }else {
      $p.text('输入正确').css("display","block").css("color","green");
    }
  })

  $("#reg").click(function () {
    var username = $texts.val();
    var password = $password.val();
    var validation = $copy_password.val();


    $.ajax({
      type: "POST",
      url: "/register",
      async:false,
      data: {"username":username, "password":password,"validation":validation},
      dataType: "json",
      success: function(data){
        if (data == ""||data == null){
          alert("服务器繁忙");
        }
        if (data.code == 200){
          alert(data.msg);
          setTimeout(function () {
            location.href = data.data;
          },1000);
        }else {
          alert(data.msg);
        }
      }
    })
  })

})




